package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.base.EamAssetBrandQuery;
import com.jcfk.eam.domain.dto.base.EamAssetBrandDTO;
import com.jcfk.eam.domain.uploadExcel.base.EamAssetBrandUploadExcel;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import com.jcfk.eam.service.base.EamAssetBrandService;
import com.jcfk.common.core.utils.ExcelUtils;

/**
 * 资产品牌
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-24
 */
@Api(tags = {"资产品牌管理"})
@RestController
@RequestMapping("eamassetbrand")
public class EamAssetBrandController {
    @Autowired
    private EamAssetBrandService eamAssetBrandService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamAssetBrandQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询资产品牌")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetBrandDTO> queryByPage(@RequestBody ReqPageParam<EamAssetBrandQuery> requestParam) {
        DataPage<EamAssetBrandDTO> page = new DataPage<EamAssetBrandDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamAssetBrandDTO.class);
        page = eamAssetBrandService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamAssetBrandQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取资产品牌列表", notes = "获取资产品牌列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamAssetBrandDTO>> getList(@RequestBody EamAssetBrandQuery query) {
        if(query.getDeleted() == null)
            query.setDeleted(0);

        if(query.getEnabled() == null)
            query.setEnabled(1);

//        List<EamAssetBrandDTO> list = eamAssetBrandService.getList(query);

        List<EamAssetBrandDTO> list = eamAssetBrandService.queryByPage(query);
        return RetResult.ok(list);
    }

    /**
     *  资产品牌
     * 根据id 查询详情
     */
    @ApiOperation("查询资产品牌")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetBrandDTO> get(@PathVariable("id") String id) {
            EamAssetBrandDTO item = eamAssetBrandService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产品牌
     */
    @ApiOperation("保存资产品牌")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产品牌管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetBrandDTO eamAssetBrand) {
        // 数据校验
        RetResult ret = eamAssetBrandService.isValid(eamAssetBrand);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if(1 == eamAssetBrand.getEnabled()) {
            EamAssetBrandQuery query = new EamAssetBrandQuery();
            query.setBrandCode(eamAssetBrand.getBrandCode());
            query.setMakerId(eamAssetBrand.getMakerId());
            List<EamAssetBrandDTO> getList = eamAssetBrandService.getList(query);

            if (getList != null && !getList.isEmpty()) {
                for (EamAssetBrandDTO set : getList) {
                    if (!set.getId().equals(eamAssetBrand.getId()))
                        throw new CustomException("相同制造商下，品牌编号不能重复，保存失败");
                }
            }
        }
        if (StrUtil.isEmpty(eamAssetBrand.getId())){
            // 新增
            eamAssetBrandService.insert(eamAssetBrand);
        } else{
            // 更新
            eamAssetBrandService.update(eamAssetBrand);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除资产品牌
     */
    @ApiOperation("批量删除资产品牌")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产品牌管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetBrandDTO eamAssetBrand) {
        return RetResult.ok(eamAssetBrandService.logicDeleteList(eamAssetBrand));
    }

    /**
     * 导入资产品牌
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<EamAssetBrandUploadExcel> items = EasyExcel.read(file.getInputStream(),EamAssetBrandUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        eamAssetBrandService.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<EamAssetBrandUploadExcel> list = new ArrayList<>();
        list.add(new EamAssetBrandUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetBrandUploadExcel.class);
    }


    /**
     *  删除资产品牌
     */
    @ApiOperation("删除资产品牌")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产品牌管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamAssetBrandService.deleteitem(id);
        return RetResult.ok();
    }
}
